package com.zc.action;

import java.util.Date;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zc.dao.impl.ArticleDaoImpl;
import com.zc.model.Article;
import com.zc.model.User;

public class ArticleAddAction extends ActionSupport implements ModelDriven<Article> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Article article = new Article();

	@Override
	public String execute() throws Exception {
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		article.setCreator(((User) session.get("user")).getUsername());
		article.setCrateTime(new Date().toLocaleString());
		article.setLastestEditor(((User) session.get("user")).getUsername());
		article.setLastestEditTime(article.getCrateTime());
		ArticleDaoImpl articleDao = new ArticleDaoImpl();
		if (articleDao.addArticle(article)) {
			session.remove("articles");
			session.put("articles", articleDao.getAllArticle());
			return SUCCESS;
		} else {
			return ERROR;
		}
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	@Override
	public Article getModel() {
		// TODO Auto-generated method stub
		return null;
	}

}
